/*
 * FindReplacePanel
 * Copyright (C) 2011, Aleksey Nikiforov
 *
 * This file is part of FindReplacePanel.
 *
 * FindReplacePanel is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * FindReplacePanel is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package simplex3d.console.findreplace;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.prefs.AbstractPreferences;
import java.util.prefs.BackingStoreException;


/**
 *
 * @author Aleksey Nikiforov (lex)
 */
public class TempPreferences extends AbstractPreferences implements Serializable {

    private static long serialVersionUID = 1111L;
    

    private static final TempPreferences root = new TempPreferences(null, "");
    private static long counter = 0;

    public static synchronized TempPreferences userNodeForPackage(Class clazz) {
        if (clazz == null) {
            String name = "$tempPrefs$" + counter;
            counter += 1;
            return root.childSpi(name);
        }
        else {
            String name = clazz.getName();
            ArrayList<String> tokens = new ArrayList<String>();

            int idx = 0;
            boolean parse = true;
            while (parse && idx < name.length()) {
                int found = name.indexOf('.', idx);
                if (found > 0) {
                    tokens.add(name.substring(idx, found));
                    idx = found + 1;
                }
                else {
                    parse = false;
                }
            }

            TempPreferences currentNode = root;
            for (String child : tokens) {
                currentNode = currentNode.childSpi(child);
            }

            return currentNode;
        }
    }


    private HashMap<String, String> values = new HashMap<String, String>();
    private ArrayList<TempPreferences> children = new ArrayList<TempPreferences>();

    private TempPreferences(TempPreferences parent, String name) {
        super(parent, name);
    }


    @Override
    protected void putSpi(String key, String value) {
        values.put(key, value);
    }

    @Override
    protected String getSpi(String key) {
        return (String) values.get(key);
    }

    @Override
    protected void removeSpi(String key) {
        values.remove(key);
    }

    @Override
    protected void removeNodeSpi() throws BackingStoreException {
        values = null;
    }

    @Override
    protected String[] keysSpi() throws BackingStoreException {
        return (String[]) values.keySet().toArray();
    }

    @Override
    protected String[] childrenNamesSpi() throws BackingStoreException {
        String[] names = new String[children.size()];
        for (int i = 0; i < names.length; i++) {
            names[i] = children.get(i).name();
        }
        return names;
    }

    @Override
    protected TempPreferences childSpi(String name) {
        for (int i = 0; i < children.size(); i++) {
            TempPreferences child = children.get(i);
            if (child.name().equals(name)) return child;
        }
        TempPreferences child = new TempPreferences(this, name);
        children.add(child);
        return child;
    }

    @Override
    protected void syncSpi() throws BackingStoreException {
        // Do nothing.
    }

    @Override
    protected void flushSpi() throws BackingStoreException {
        // Do nothing.
    }
}
